<template>
	<view>
		<view class="back">
			<text class="lg text-gray cuIcon-back"></text>
		</view>
		<view class="login_title">
			欢迎登录！
		</view>
		<view class="login_detail">
			<form>
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="用户名/电话"
				></wInput>
				<view class="password">
					<wInput
						v-model="passData"
						type="password"
						maxlength="11"
						placeholder="登录密码"
						isShowPass
					></wInput>
				</view>
				<wButton
					text="登 录"
					:rotate="isRotate" 
					@click.native="startLogin()"
				></wButton>
				
			</form>
			
			<view class="forget">
				<span>忘记密码?</span>
			</view>
			
			<view class="to_signin">
                还没有账号，<span @tap="navigateToSignin()">立即注册</span>
			</view>
			
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	export default {
		components: {
			wInput,
			wButton,
		},
		data() {
			return {
				phoneData:'', //用户/电话
				passData:'', //密码
				isRotate: false, //是否加载旋转
			}
		},
		mounted() {
			_this= this;
			//this.isLogin();
		},
		methods: {
			navigateToSignin(){
				uni.navigateTo({
					url:'../register/register'
				})
			},
			isLogin(){
				//判断缓存中是否登录过，直接登录
				// try {
				// 	const value = uni.getStorageSync('setUserData');
				// 	if (value) {
				// 		//有登录信息
				// 		console.log("已登录用户：",value);
				// 		_this.$store.dispatch("setUserData",value); //存入状态
				// 		uni.reLaunch({
				// 			url: '../../../pages/index',
				// 		});
				// 	}
				// } catch (e) {
				// 	// error
				// }
			},
			startLogin(){
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return;
				}
			    if (this.passData.length < 5) {
			        uni.showToast({
			            icon: 'none',
						position: 'bottom',
			            title: '密码不正确'
			        });
			        return;
			    }
				uni.request({
					url:"http://221.234.36.126:82/api.php/Login/login",
					method:"POST",
					data:{
						user_phone:this.phoneData,
						user_pwd:this.passData,
						token:'1CECE2221211DDCB613882C3311EC670'
					},
					header: {
						'content-type': 'application/json;charset=UTF-8' 
					},
					success: (res) => {
						if(res.data.code==1){
							uni.showToast({
							    icon: 'none',
								position: 'bottom',
							    title: res.data.msg
							});
							setTimeout(function(){
								uni.switchTab({
								    url: '/pages/index/index'
								});
							},3000);
							uni.setStorageSync('user_info',res.data.data);
						}
						
						console.log(res.data);
					}
					
				})
				// console.log("登录成功")
				
				_this.isRotate=true
				setTimeout(function(){
					_this.isRotate=false
				},3000)
				// uni.showLoading({
				// 	title: '登录中'
				// });
				// getLogin()
				// .then(res => {
				// 	//console.log(res)
				// 	//简单验证下登录（不安全）
				// 	if(_this.phoneData==res.data.username && _this.passData==res.data.password){
				// 		let userdata={
				// 			"username":res.data.username,
				// 			"nickname":res.data.nickname,
				// 			"accesstoken":res.data.accesstoken,
				// 		} //保存用户信息和accesstoken
				// 		_this.$store.dispatch("setUserData",userdata); //存入状态
				// 		try {
				// 			uni.setStorageSync('setUserData', userdata); //存入缓存
				// 		} catch (e) {
				// 			// error
				// 		}
				// 		uni.showToast({
				// 			icon: 'success',
				// 			position: 'bottom',
				// 			title: '登录成功'
				// 		});
				// 		uni.reLaunch({
				// 			url: '../../../pages/index',
				// 		});
				// 	}else{
				// 		_this.passData=""
				// 		uni.showToast({
				// 			icon: 'error',
				// 			position: 'bottom',
				// 			title: '账号或密码错误，账号admin密码admin'
				// 		});
				// 	}
				// 	uni.hideLoading();
				// }).catch(err => {
				// 	uni.hideLoading();
				// })
				
			}
			,
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
		}
	}
</script>

<style>
	.content{
		padding-top: 30rpx;
	}
	
	.back{
		font-size: 32rpx;
		/* margin-top: 35rpx; */
		margin-left: 24rpx;
		width: 32rpx;
	}
	
	.back text{
		font-size: 32rpx;
		color: #333333;
	}
	
	.login_title{
		width: 100%;
		font-size: 40rpx;
		color: #333333;
		box-sizing: border-box;
		padding-left: 30rpx;
		margin-top: 70rpx;
		font-weight: 600;
		letter-spacing: 3rpx;
	}
	
	.login_detail{
		width: 100%;
		box-sizing: border-box;
		padding: 0 30rpx;
		margin-top: 90rpx;
	}
	
	
	
	.login_detail button{
		width: 100%;
		height: 90rpx;
		border-radius: 45rpx;
		background-color: #00A7FF;
		color: #FFFFFF;
		font-size: 40rpx;
		margin-top: 70rpx;
		letter-spacing: 5rpx;
	}
	
	.forget{
		width: 100%;
		text-align: right;
		margin-top: 35rpx;
	}
	
	.forget span{
		font-size: 26rpx;
		color: #999999;
	}
	
	.to_signin{
		width: 100%;
		text-align: center;
		font-size: 26rpx;
		color: #999999;
		margin-top: 40rpx;
	}
	
	.to_signin span{
		color: #00A7FF;
	}
</style>
